package com.example.demo.Service;

import com.example.demo.Dao.PersonRepository;
import com.example.demo.Entity.Person;
import com.example.demo.Security.CustomUserDeatailImpl;
import com.example.demo.Utils.JavaWebTokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AuthService {

    @Resource
    private PersonRepository personRepository;




    public Person registerUser(Person userToAdd ) {

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        final String rawPassword = userToAdd.getPassword();
        userToAdd.setPassword( encoder.encode(rawPassword) );
        if (userToAdd.getRole()==null)
        {
            userToAdd.setRole("ROLE_USER");
        }
        return personRepository.save(userToAdd);
    }
    public Person registerWorker(Person userToAdd ) {

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        final String rawPassword = userToAdd.getPassword();
        userToAdd.setPassword( encoder.encode(rawPassword) );
        if (userToAdd.getRole()==null)
        {
            userToAdd.setRole("ROLE_WORKER");
        }
        return personRepository.save(userToAdd);
    }

 }
